package com.sicpa.common.utils;

import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * EncryptUtils
 *
 * @author zhangjian
 * @date 2020/03/10 14:23
 */
public class EncryptUtils {

    public static String hmacSha1(String toEnc, String key) {
        HmacUtils hmacUtils = new HmacUtils(HmacAlgorithms.HMAC_SHA_1, key);
        return hmacUtils.hmacHex(toEnc);
    }

    public static String urlEncode(String str) {
        return urlEncode(str, StandardCharsets.UTF_8);
    }

    public static String urlEncode(String str, Charset charset) {
        try {
            return URLEncoder.encode(str, charset.toString());
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

}
